//logs.js
var util = require('../../utils/util.js')
Page({
  data: {
     item: {
        display    :'hide',
        error_info :'请输入比赛阶段',
     },
     button:{
         disabled:false
     },
     event_stage  : '',
     sum_frame    : '',
     player1_name : '',
     player2_name : '',
     id           : '',
     player1_score: '',
     player2_score: '',
  },
    //获取对阵的信息
  onShow:function(){
     var battle_info = wx.getStorageSync('battle_info');
     console.log(battle_info);
     this.setData({
        event_stage  : battle_info.event_stage,
        sum_frame    : battle_info.sum_frame,
        player1_name : battle_info.player1_name,
        player2_name : battle_info.player2_name,
        player1_score: wx.getStorageSync('player1_score'),
        player2_score: wx.getStorageSync('player2_score'),
        id           : battle_info.battle_id
     })
  },

  //输入框值改变触发
  changeEventStage:function(e){
       this.data.event_stage = e.detail.value;
  },
  changeSumFrame:function(e){
       this.data.sum_frame = e.detail.value;
  },
  changePlayer1Name:function(e){
       this.data.player1_name = e.detail.value;
  },
  changePlayer2Name:function(e){
       this.data.player2_name = e.detail.value;
  },

  //点击保存按钮触发
   doSave:function(){
      var that = this;
      //验证用户的输入
      if(this.checkInput()){
          that.setDisabled();
          wx.showLoading({
              title: '保存中...',
          });

          var url  = getApp().data.servser+'/Battle/edit';
          var battle_info = wx.getStorageSync('battle_info');
          wx.request({
            url: url,
            data: {
              'id'          : that.data.id,
              'event_stage' : that.data.event_stage,
              'sum_frame'   : that.data.sum_frame,
              'player1_name': that.data.player1_name,
              'player2_name': that.data.player2_name,
              'battle_type' : battle_info.battle_type
            },
            header: {
                'content-type': 'application/json'
            },
            method : "GET",
            success: function(res) {
               wx.hideLoading();
               console.log(res);
               if(res.data.state == 1){
                   //将修改后的对局信息保存在本地缓存中
                   console.log(res);
                   that.saveBattleInfo(that.data.id);
                   wx.navigateBack(1);
               }else{
                   that.setDisabled();
                   that.setData({
                      item: {
                          display    :'show',
                          error_info : res.data.info
                      },
                  })
                  that.closeErrorInfo();
               }
            }
          })
      }
   },

   //对用户的输入进行验证
   checkInput:function(){
     var that = this;
     var error_info = '';
     if (this.data.event_stage == '') {
       error_info = '请输入比赛阶段';
     } else if (this.data.sum_frame == '') {
       error_info = '请输入总局数';
     } else if (this.data.player1_name == '') {
       error_info = '请输入蓝角选手姓名';
     } else if (this.data.player2_name == '') {
       error_info = '请输入红角选手姓名';
     } else if (this.data.event_stage.length > 6) {
       error_info = '比赛阶段字数不能大于6';
     } else if (isNaN(this.data.sum_frame)) {
       error_info = "总局数只能为1-100的数字"
     } else if (Number(this.data.sum_frame) < 1 || Number(this.data.sum_frame) > 100) {
       error_info = "总局数只能为1-100的数字"
     } else {
       if (this.data.player1_name.indexOf(',') > 0) {
         var player1_name_array = this.data.player1_name.split(",");
         if (player1_name_array[0].length > 4 || player1_name_array[1].length > 4) {
           error_info = "红方选手姓名字数不能大于4"
         }
       } else if (this.data.player1_name.indexOf('，') > 0) {
         var player1_name_array = this.data.player1_name.split("，");
         if (player1_name_array[0].length > 4 || player1_name_array[1].length > 4) {
           error_info = "红方选手姓名字数不能大于4";
         }
       } else if (this.data.player2_name.indexOf(',') > 0) {
         var player2_name_array = this.data.player2_name.split(",");
         if (player2_name_array[0].length > 4 || player2_name_array[1].length > 4) {
           error_info = "蓝方选手姓名字数不能大于4"
         }
       } else if (this.data.player2_name.indexOf('，') > 0) {
         var player2_name_array = this.data.player2_name.split("，");
         if (player2_name_array[0].length > 4 || player2_name_array[1].length > 4) {
           error_info = "蓝方选手姓名字数不能大于4";
         }
       }
       if (error_info == '') {
         if (this.data.player1_name.indexOf(',') < 0 && this.data.player1_name.indexOf('，') < 0 && this.data.player1_name.length > 4) {
           error_info = "红选手姓名字数不能大于4"
         } else if (this.data.player2_name.indexOf(',') < 0 && this.data.player2_name.indexOf('，') < 0 && this.data.player2_name.length > 4) {
           error_info = "蓝方选手姓名字数不能大于4"
         }
       }
     }
       if(error_info !=''){
           that.setData({
              item: {
                  display    :'show',
                  error_info :error_info
               }
            })
            that.closeErrorInfo();
            return false;
       }
       return true;
   },
   //定时器关闭提示信息
    closeErrorInfo:function(){
        var that = this;
        setTimeout(function(){
          that.setData({
              item: {display:'hide'}
              })
        },'2000')
    },

    //设置button的启用与失效
    setDisabled: function() {
        this.setData({
           button:{
               disabled: !this.data.button.disabled
           }
        })
    }, 

    //保存对局的信息到本地缓存
    saveBattleInfo:function(battle_id){
        var that = this;
        
        var battle_info = wx.getStorageSync('battle_info');
        wx.setStorage({
            key:"battle_info",
            data:{
                battle_id     : battle_id,
                event_stage   : that.data.event_stage,
                sum_frame     : that.data.sum_frame,
                player1_name  : that.data.player1_name,
                player2_name  : that.data.player2_name,
                battle_type   : battle_info.battle_type
            }
        });
    } 

   
})
